package com.baiyangtx.lingerweather.view;

import java.util.Calendar;

import com.baiyangtx.lingerweather.model.DefaultWeatherChartModel;
import com.baiyangtx.lingerweather.model.WeatherChartModel;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class WeatherChart extends RelativeLayout {

	private int textSize = 20;

	private int textColor = 0xFFFFFFFF;

	private int textPaddingLeft = 30;

	private int textPaddingRight = 20;

	private int textPaddingTop = 5;

	private int textPaddingBotton = 5;

	private int idReference = 100;

	private WeatherChartModel model;

	
	
	
	
	/*
	 * 第一行 7天的星期数
	 */
	private TextView[] weekdays = new TextView[7];

	/*
	 * 第二行 7天的中午天气文字
	 */
	private TextView[] noonWeathersTxt = new TextView[7];

	/*
	 * 第三行 7天的中午天气图标
	 */
	private ImageView[] noonWeathersPic = new ImageView[7];

	/*
	 * 7天的夜间天气图标
	 */
	private ImageView[] nightWeathersPic = new ImageView[7];

	/*
	 * 7天的夜间天气文字
	 */
	private TextView[] nightWeathersTxt = new TextView[7];

	/*
	 * 这7天的日期
	 */
	private TextView[] days = new TextView[7];

	
	private TemperatureChart temperatureChart = null ;
	
	
	
	private Context context;

	public WeatherChart(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;

		DefaultWeatherChartModel model = new DefaultWeatherChartModel(context);
		this.model = model;
		model.setToday(Calendar.getInstance());
		
		
		temperatureChart = new TemperatureChart(context) ;
		temperatureChart.setModel(model) ;
		
		
		
		initSuface(context);
		updataSuface() ;
	}

	public void setWeatherChartModel(WeatherChartModel model) {
		this.model = model;
	}

	public WeatherChartModel getWeatherChartModel() {
		return model;
	}

	
	
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		
		int width = (textSize * 3 + textPaddingLeft + textPaddingRight ) * 7;
		setMeasuredDimension( width , MeasureSpec.getSize(heightMeasureSpec));
		
	}
	
	
	
	
	
	
	
	
	
	
	
	public void updataSuface()
	{
		for( int i = 0 ; i < model.getDaysNumber()  ; i++ )
		{
			weekdays[i].setText( model.getDayOfWeek(i) ) ;
			noonWeathersTxt[i].setText(  model.getNoonWeatherText(i) ) ;
			nightWeathersTxt[i].setText( model.getNightWeatherText(i) ) ;
			days[i].setText( model.getDate(i) );
			
			noonWeathersPic[i].setImageDrawable( model.getNoonWeatherDrawable(i) ) ;
		//	nightWeathersPic[0].setBackgroundColor(textColor) ;
			nightWeathersPic[i].setImageDrawable( model.getNightWeatherDrawable(i) ) ;
		}
	}
	
	
	
	
	
	private void initSuface(Context context) {
		initViews() ;	
		layoutViews() ;	
		addViews() ;
	}

	private void initViews() {
		for (int i = 0; i < weekdays.length; i++) {
			weekdays[i] = new TextView(context);
			weekdays[i].setId(idReference + i);

			noonWeathersTxt[i] = new TextView(context);
			noonWeathersTxt[i].setId(idReference + model.getDaysNumber() * 1
					+ i);

			noonWeathersPic[i] = new ImageView(context);
			noonWeathersPic[i].setId(idReference + model.getDaysNumber() * 2
					+ i);

			nightWeathersPic[i] = new ImageView(context);
			nightWeathersPic[i].setId(idReference + model.getDaysNumber() * 3
					+ i);

			nightWeathersTxt[i] = new TextView(context);
			nightWeathersTxt[i].setId(idReference + model.getDaysNumber() * 4
					+ i);

			days[i] = new TextView(context);
			days[i].setId(idReference + model.getDaysNumber() * 5 + i);
		}
		
		temperatureChart.setId(idReference + model.getDaysNumber() * 6) ;
	}

	
	
	private void addViews()
	{
		for (int i = 0; i < weekdays.length; i++) {
			addView(weekdays[i]);
			addView(noonWeathersTxt[i]);
			addView(noonWeathersPic[i]);
			
			addView(days[i]);
			addView(nightWeathersPic[i]);
			addView(nightWeathersTxt[i]);
		}
		
		addView(temperatureChart) ;
	}
	
	
	
	
	/*
	 * 
	 */
	private void layoutViews() {
//		LayoutParams reference = new LayoutParams(LayoutParams.WRAP_CONTENT,
//				LayoutParams.WRAP_CONTENT);
//		reference.addRule(ALIGN_PARENT_LEFT);
//		reference.addRule(ALIGN_PARENT_TOP);
		for (int i = 0; i < model.getDaysNumber(); i++) {
			layoutViewsLine(i) ;
		}
		
		LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT ,
				LayoutParams.MATCH_PARENT);
		params.addRule( BELOW , noonWeathersPic[1].getId() ) ;
		params.addRule( ALIGN_LEFT , weekdays[0].getId() ) ;
		params.addRule(ABOVE , nightWeathersPic[0].getId() ) ;
	//	params.
		temperatureChart.setLayoutParams(params) ;
	//	temperatureChart.set
	}

	private void layoutViewsLine(int clos) {
		if (clos == 0) {
			setViewParam(weekdays[clos], false, getId(), true, getId());
		} else {
			setViewParam(weekdays[clos], false, weekdays[clos - 1].getId(),
					true, getId());
		}
		
		setViewParam(noonWeathersTxt[clos], true, weekdays[clos].getId(),
				true, weekdays[clos].getId() );
		setViewParam(noonWeathersPic[clos], true, weekdays[clos].getId(),
				true, noonWeathersTxt[clos].getId() );
		
		setViewParam(days[clos], true, weekdays[clos].getId(),
				false , getId() );
		setViewParam(nightWeathersTxt[clos], true, weekdays[clos].getId(),
				false , days[clos].getId() );
		setViewParam(nightWeathersPic[clos], true, weekdays[clos].getId(),
				false , nightWeathersTxt[clos].getId() );
	}

	
	
	/*
	 * 
	 */
	private void setViewParam(View view, boolean isAlignViewLeft, int alignId,
			boolean isBelow, int belowOrAboutID) {
		LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);

		if (alignId == getId()) {
			params.addRule(ALIGN_PARENT_LEFT);
		} else {
			int verb = isAlignViewLeft ? ALIGN_LEFT : RIGHT_OF;
			params.addRule(verb, alignId);
		}

		if (belowOrAboutID == getId()) {
			int verb = isBelow ? ALIGN_PARENT_TOP : ALIGN_PARENT_BOTTOM;
			params.addRule(verb);
		} else {
			int verb = isBelow ? BELOW : ABOVE;
			params.addRule(verb, belowOrAboutID);
		}
		view.setLayoutParams(params);
		view.setPadding(textPaddingLeft, textPaddingTop, textPaddingRight,
				textPaddingBotton);
		
		if (view instanceof TextView) {
			
			((TextView) view).setTextColor(textColor);
			((TextView) view).setTextSize(textSize);
			((TextView) view).setGravity(Gravity.CENTER_VERTICAL) ;
		}else if( view instanceof ImageView ){
			// 虽然代码不够优雅，但是这里就这样吧，懒得去改了
			view.setPadding(textPaddingRight, textPaddingTop, textPaddingRight,
					textPaddingBotton);
		}
	}


}
